﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SMSManage
{
    public partial class frmManageSchedule : Form
    {
        #region constructorAndVariables
        /// <summary>
        /// Constructor
        /// </summary>
        public frmManageSchedule()
        {
            InitializeComponent();
        }

        #endregion constructorAndVariables

        #region loadInitializeData

        /// <summary>
        /// Form loaded
        /// </summary>
        private void frmManageSchedule_Load(object sender, EventArgs e)
        {
            BindData();
        }

        /// <summary>
        /// Bind data
        /// </summary>
        private void BindData()
        {
            // TODO: This line of code loads data into the 'schedule.tbl_Schedule_Select_All' table. You can move, or remove it, as needed.
            this.tbl_Schedule_Select_AllTableAdapter.Fill_tbl_Schedule_Select_All(this.schedule.tbl_Schedule_Select_All);

            //Enable ID textfield
            this.txt_ID.Enabled = true;

            //Set default selected item for fields
            this.cbb_Status.SelectedItem = "Kích hoạt";
            this.txt_ID.Text = String.Empty;
            this.txt_Name.Text = String.Empty;
            this.txt_Desc.Text = String.Empty;
            this.dtpk_Time.Value = DateTime.Now;

            //Disable, enable buttons
            this.btn_Add.Enabled = true;
            this.btn_Search.Enabled = true;
            this.btn_Update.Enabled = false;
            this.btn_Del.Enabled = false;

            //Fill lop hoc
            this.tbl_ClassTableAdapter.Fill_tbl_Class_Select_All(this._class.tbl_Class);

            // TODO: This line of code loads data into the 'subject.tbl_Subject' table. You can move, or remove it, as needed.
            this.tbl_SubjectTableAdapter.Fill_tbl_Subject_Select_All(this.subject.tbl_Subject);

            // TODO: This line of code loads data into the 'location.tbl_Location' table. You can move, or remove it, as needed.
            this.tbl_LocationTableAdapter.Fill_tbl_Location_Select_All(this.location.tbl_Location);

        }

        /// <summary>
        /// Bound data completely
        /// </summary>
        private void dtGrv_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            //Coded by Tuan
            for (int i = 0; i < this.dtGrv.Rows.Count; i++)
            {
                try
                {
                    this.dtGrv.Rows[i].Cells["TT"].Value = (i + 1).ToString(); //Thu tu
                }
                catch (Exception exc)
                {
                    exc.ToString();
                }
            }
            this.dtGrv.Refresh();
        }

        #endregion loadInitializeData

        #region exit
        /// <summary>
        /// exit
        /// </summary>
        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        #endregion exit

        #region updateDelete

        /// <summary>
        /// Cell clicked
        /// </summary>
        private void dtGrv_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex > -1)
                {
                    //Enable buttons
                    this.btn_Update.Enabled = true;
                    this.btn_Del.Enabled = true;

                    //disable buttons
                    this.btn_Add.Enabled = false;
                    this.btn_Search.Enabled = false;
                    int ScheduleID = Convert.ToInt32(this.dtGrv.Rows[e.RowIndex].Cells["ScheduleID"].Value);

                    this.cbb_Subject.SelectedValue = this.dtGrv.Rows[e.RowIndex].Cells["SubjectID"].Value;
                    this.cbb_Location.SelectedValue = this.dtGrv.Rows[e.RowIndex].Cells["LocationID"].Value;
                    this.cbb_Class.SelectedValue = this.dtGrv.Rows[e.RowIndex].Cells["ClassID"].Value;

                    //Disable ID text field
                    this.txt_ID.Enabled = false;

                    this.txt_ID.Text = ScheduleID.ToString();
                    this.txt_Name.Text = this.dtGrv.Rows[e.RowIndex].Cells["ScheduleName"].Value.ToString();
                    this.txt_Desc.Text = this.dtGrv.Rows[e.RowIndex].Cells["ScheduleDescription"].Value.ToString();
                    this.dtpk_Time.Value = Convert.ToDateTime(this.dtGrv.Rows[e.RowIndex].Cells["ScheduleTime"].Value);

                    //change selected value in combobox
                    switch (this.dtGrv.Rows[e.RowIndex].Cells["ScheduleStatus"].Value.ToString())
                    {
                        case "1":
                            this.cbb_Status.SelectedItem = "Kích hoạt";
                            break;
                        case "0":
                            this.cbb_Status.SelectedItem = "Khóa";
                            break;
                        default:
                            this.cbb_Status.SelectedItem = "Kích hoạt";
                            break;
                    }
                }
            }
            catch (Exception exc)
            {
                exc.ToString();
            }
        }

        /// <summary>
        /// Update
        /// </summary>
        private void btn_Update_Click(object sender, EventArgs e)
        {
            try
            {
                DatasetXSD.ScheduleTableAdapters.tbl_Schedule_Select_AllTableAdapter dp =
                    new DatasetXSD.ScheduleTableAdapters.tbl_Schedule_Select_AllTableAdapter();
                int status = 0;

                switch (this.cbb_Status.SelectedItem.ToString())
                {
                    case "Kích hoạt":
                        status = 1;
                        break;
                    case "Khóa":
                        status = 0;
                        break;
                    default:
                        status = 1;
                        break;
                }
                if (MessageBox.Show("Bạn có thực sự muốn cập nhật buổi học này?", "Xác nhận",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    int ok = dp.tbl_Schedule_Update_All(this.txt_Name.Text.Trim(),
                        this.dtpk_Time.Value,
                        Convert.ToInt32(this.cbb_Class.SelectedValue.ToString()),
                        Convert.ToInt32(this.cbb_Location.SelectedValue.ToString()),
                        Convert.ToInt32(this.cbb_Subject.SelectedValue.ToString()),
                        this.txt_Desc.Text.Trim(),
                        status,
                        Convert.ToInt32(this.txt_ID.Text.Trim()));
                    //Nếu có hơn 0 bản ghi được cập nhật
                    if (ok > 0)
                    {
                        MessageBox.Show("Cập nhật thành công", "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        BindData();
                        this.dtGrv.Refresh();
                    }
                    else
                    {
                        MessageBox.Show("Cập nhật thất bại", "Thất bại", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }

            }
            catch (Exception exc)
            {
                exc.ToString();
                MessageBox.Show("Có lỗi xảy ra, cập nhật thất bại", "Có lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        /// <summary>
        /// Delete
        /// </summary>
        private void btn_Del_Click(object sender, EventArgs e)
        {
            try
            {
                DatasetXSD.ScheduleTableAdapters.tbl_Schedule_Select_AllTableAdapter dp
                    = new DatasetXSD.ScheduleTableAdapters.tbl_Schedule_Select_AllTableAdapter();
                if (MessageBox.Show("Bạn có thực sự muốn xóa buổi học này?", "Xác nhận",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    int ok =
                        dp.tbl_Schedule_Delete_By_ScheduleID(Convert.ToInt32(this.txt_ID.Text.Trim()));

                    //Nếu có nhiều hơn 0 bản ghi được xóa
                    if (ok > 0)
                    {
                        MessageBox.Show("Xóa thành công", "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        BindData();
                        this.dtGrv.Refresh();
                    }
                    else
                    {
                        MessageBox.Show("Xóa thất bại", "Thất bại", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            catch (Exception exc)
            {
                exc.ToString();
                MessageBox.Show("Có lỗi xảy ra, xóa thất bại", "Có lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        #endregion updateDelete

        #region Search

        /// <summary>
        /// Search
        /// </summary>
        private void btn_Search_Click(object sender, EventArgs e)
        {
            try
            {
                string status = String.Empty;
                switch (this.cbb_Status.SelectedItem.ToString())
                {
                    case "Kích hoạt":
                        status = "1";
                        break;
                    case "Khóa":
                        status = "0";
                        break;
                    default:
                        status = String.Empty;
                        break;
                }

                DatasetXSD.ScheduleTableAdapters.tbl_Schedule_Select_AllTableAdapter dp =
                    new DatasetXSD.ScheduleTableAdapters.tbl_Schedule_Select_AllTableAdapter();

                this.dtGrv.DataSource = dp.Get_tbl_Schedule_Search_By_All(this.txt_ID.Text.Trim(),
                    this.txt_Name.Text.Trim(),
                    this.dtpk_Time.Value,
                    this.cbb_Class.SelectedValue.ToString(),
                    this.cbb_Location.SelectedValue.ToString(),
                    this.cbb_Subject.SelectedValue.ToString(),
                    this.txt_Desc.Text.Trim(),
                    status);
                this.dtGrv.Refresh();

            }
            catch (Exception exc)
            {
                exc.ToString();
                MessageBox.Show("Có lỗi xảy ra, tìm kiếm thất bại", "Có lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        #endregion Search

        #region Add
        /// <summary>
        /// Add
        /// </summary>
        private void btn_Add_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Bạn có thực sự muốn thêm mới buổi học này?", "Xác nhận",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    DatasetXSD.ScheduleTableAdapters.tbl_Schedule_Select_AllTableAdapter dp
                        = new DatasetXSD.ScheduleTableAdapters.tbl_Schedule_Select_AllTableAdapter();
                    int status = 0;
                    switch (this.cbb_Status.SelectedItem.ToString())
                    {
                        case "Kích hoạt":
                            status = 1;
                            break;
                        case "Khóa":
                            status = 0;
                            break;
                        default:
                            status = 1;
                            break;
                    }
                    int ok = dp.tbl_Schedule_Insert_All(this.txt_Name.Text.Trim(),
                        this.dtpk_Time.Value,
                        Convert.ToInt32(this.cbb_Class.SelectedValue.ToString()),
                        Convert.ToInt32(this.cbb_Location.SelectedValue.ToString()),
                        Convert.ToInt32(this.cbb_Subject.SelectedValue.ToString()),
                        this.txt_Desc.Text.Trim(),
                        status);

                    if (ok > 0)
                    {
                        //Báo thành công
                        MessageBox.Show("Thêm mới thành công", "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        BindData();
                        this.dtGrv.Refresh();
                    }
                    else
                    {
                        //Báo thất bại
                        MessageBox.Show("Thêm mới thất bại", "Thất bại", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }

                }
            }
            catch (Exception exc)
            {
                exc.ToString();
                MessageBox.Show("Có lỗi xảy ra, thêm mới thất bại", "Có lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        #endregion Add

    }
}
